package com.jwb.pub.controller;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.service.UploadFileService;
import com.jwb.util.MainController;
@Slf4j
@Controller
@RequestMapping(value="/pub/file")
public class UploadFileController extends MainController{	

	@Autowired
	private BaseDAO baseDAO;  
	@Autowired
	private UploadFileService uploadFileService;
	
	@RequestMapping(value="/getMaxId")    
    @ResponseBody
    public String  getMaxId(HttpServletRequest req,HttpServletResponse response) throws Exception {
    	JSONObject jsobject = new JSONObject();      
    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {
    		jsdata=JSONObject.parseObject(jsonStr);
	    	int newId=baseDAO.getMaxId("upload_file", "id");
	    	if(newId>0)
	    	{
	    		jsobject.put("id", newId);
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "");	
	    	}
	    	else
	    	{
	    	
				jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "");	
	    	}
    	}
    	 catch (Exception e) {
 			e.printStackTrace();
 			jsobject.put("respCode", "-3");
 			jsobject.put("respDesc", "字符串格式有误");	

 		}

    	log.info("getMaxId():" + jsobject.toString());
    	return jsobject.toString();	
    }
	
	/**
	 * 
	 * @param req
	 * @param response
	 * @return
	 * @throws Exception
	 */
	 @RequestMapping(value="/upfileSave")    
	    @ResponseBody
	    public String  upfileSave(HttpServletRequest req,HttpServletResponse response) throws Exception {
	    	JSONObject jsobject = new JSONObject();  
	    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
	    	JSONObject jsdata = new JSONObject();
	    	try {

				jsdata=JSONObject.parseObject(jsonStr);
				jsobject = uploadFileService.upfileSave(jsdata);
			
	    	
			} catch (Exception e) {
				e.printStackTrace();
				jsobject.put("respCode", "-3");
				jsobject.put("respDesc", "字符串格式有误");	

			}
	    	log.info("upfileSave():" + jsobject.toString());
	    	return jsobject.toString();	
	    }
	
	    /**
	     * http://127.0.0.1:8080/ssm/pub/file/upfileInfo?jsonStr={"id":"1"}
	     * @param req
	     * @param response
	     * @return
	     * @throws Exception
	     */
	    @RequestMapping(value="/upfileInfo")    
	    @ResponseBody
	    public String  upfileInfo(HttpServletRequest req,HttpServletResponse response) throws Exception {
	    
	    	JSONObject jsobject = new JSONObject();  	   
	    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
	    	JSONObject jsdata = new JSONObject();
	    	try {
				jsdata=JSONObject.parseObject(jsonStr);			
				jsobject = uploadFileService.upfileInfo(jsdata);
	    	
			} catch (Exception e) {
				e.printStackTrace();
				jsobject.put("respCode", "-3");
				jsobject.put("respDesc", "字符串格式有误");	

			}
	    	log.info("upfileInfo():" + jsobject.toString());
	        return jsobject.toString();
	    	
	    	
	    } 
	    
	    
	    /**
	     * http://127.0.0.1:8080/ssm/pub/file/uplist
	     * @param req
	     * @return
	     * @throws Exception
	     */
	    @RequestMapping(value="/uplist")	 
	    @ResponseBody
	    public String uplist(HttpServletRequest req) throws Exception
	    {   
			JSONObject jsobject = new JSONObject();   	    	
	    	String currentPageNbr= req.getParameter("currentPageNbr") == null ? "1" : req.getParameter("currentPageNbr");
	    	String pageSize= req.getParameter("pageSize") == null ? "10" : req.getParameter("pageSize");
	    	String jsonStr = req.getParameter("jsonStr") == null ? "{}" : req.getParameter("jsonStr");
	    	JSONObject jsdata = new JSONObject();
	    	try {

				jsdata=JSONObject.parseObject(jsonStr);
				
				this.pageInit();
				this.getPage().setEveryPageCnt(Integer.parseInt(pageSize));
				this.getPage().setCurrentPageNbr(Integer.parseInt(currentPageNbr));
				
				jsobject = uploadFileService.uplist(jsdata, page);
	    	
			} catch (Exception e) {
				e.printStackTrace();
				jsobject.put("respCode", "-3");
				jsobject.put("respDesc", "字符串格式有误");	

			}
	    	log.info("uplist():" + jsobject.toString());
	        return jsobject.toString();
		}	  
	    
	    
	    
	    @RequestMapping(value="/upfileDel")
		@ResponseBody
		public JSONObject upfileDel(HttpServletRequest req) throws Exception
		{
			JSONObject jsobject = new JSONObject();	
			JSONObject jsdata = new JSONObject();	
			try
			{
			    	
			    	String id = req.getParameter("id") == null ? "" : req.getParameter("id");
			    	jsdata.put("id", id);
					jsobject=uploadFileService.upfileDel(jsdata);
					
			}
			catch (Exception e)
			{
				
				jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "文件删除异常");
				
			}
			return jsobject;
		}
	       
	    
}